<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import * as signalR from '@microsoft/signalr'
import store from '@/store'
import bus from '@/utils/bus'

export default {
  name: 'App',
  created() {
    this.initSignalR()
  },
  methods: {
    initSignalR() {
      this.connection = new signalR.HubConnectionBuilder()
        .withUrl(process.env.VUE_APP_SIGNALR_API, {
          accessTokenFactory: () => store.getters.token, // 取令牌(不需要带Bearer)
          skipNegotiation: true,
          transport: signalR.HttpTransportType.WebSockets
        })
        .withAutomaticReconnect()
        .configureLogging(signalR.LogLevel.Information)
        .build()
      // 监听事件: 接收排队通知
      this.connection.on('ReceiveLineUpNotice', function(result) {
        console.log(result)
      })
      this.connection.on('ReceiveNewTodo', function(result) {
        bus.$emit('ReceiveNewTodo', result)
      })
      this.connection.start()
    }
  }
}
</script>
